﻿using Eps.Ai.Common;
using InfonavaEye.components;
using InfonavaEye.handler;
using InfonavaEye.Handler;
using InfonavaEye.util;
using Sylan.Common.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye.Forms
{
    /// <summary>
    /// 光源亮度设置
    /// </summary>
    public partial class LuminositySettingForm : Form
    {
        private LightHandler lightHandler = LightHandler.Instance;

        /// <summary>
        /// 当前亮度
        /// </summary>
        public int CurrentLuminosity { get; set; }

        public LuminositySettingForm()
        {
            InitializeComponent();
        }

        //不抢焦点,非活动窗口
        protected override bool ShowWithoutActivation => true;

        private void SettingForm_Load(object sender, EventArgs e)
        {
            if (CurrentLuminosity == 0) {
                CurrentLuminosity = Config.LIGHT_SOURCE_LUMINOSITY_DEFAULT;
            }
            this.luminosityTrackBar.Value = CurrentLuminosity;
            this.adjustLuminosity();
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            this.Invoke(new Action(() =>
            {
               
                if (luminosityTrackBar.Value > 10)
                {
                    int chageAfterVal = luminosityTrackBar.Value - 10;
                    if (chageAfterVal <= luminosityTrackBar.Minimum)
                    {
                        luminosityTrackBar.Value = luminosityTrackBar.Minimum;
                    }else {
                        luminosityTrackBar.Value -= 10;
                    }
                   // luminosityTrackBar.Value -= 10;
                    this.adjustLuminosity();
                }
            }));
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                luminosityTrackBar.Value += 10;
                this.adjustLuminosity();
            }));
        }

        /// <summary>
        /// 调整光源亮度
        /// </summary>
        private void adjustLuminosity()
        {
            lblLuminosity.Text = luminosityTrackBar.Value.ToString();
            this.CurrentLuminosity = luminosityTrackBar.Value;
            lightHandler.ChangeLuminance(luminosityTrackBar.Value);
        }

        private void luminosityTrackBar_Scroll(object sender, EventArgs e)
        {
            lblLuminosity.Text = luminosityTrackBar.Value.ToString();
        }

        private void luminosityTrackBar_MouseUp(object sender, MouseEventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                this.adjustLuminosity();
            }));
        }

        /// <summary>
        /// 点击关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
